<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/4 0004
 * Time: 15:08
 */

namespace app\currency\service;


use think\Db;

class CitiesService
{
    public static function getAddressText($id,$type = 'array',$glue = '')
    {
        $sql = "SELECT T2.id,T2.name FROM (
SELECT @r AS _id,
(SELECT @r := pid FROM cities WHERE id = _id) AS pid,
@l := @l + 1 AS lvl FROM (SELECT @r := {$id},@l := 0) vars,cities h WHERE @r <> 0
) T1 JOIN cities T2 ON T1._id = T2.id WHERE T2.pid <> 0 ORDER BY T1.lvl DESC";
        $result = Db::query($sql);
        if($type == 'array'){
            return (array)$result;
        } elseif ($type == 'string'){
            return implode($glue,array_column($result,'name'));
        } else {
            return null;
        }
    }
}